/*
 * PanelKirimPesan.java
 *
 * Created on 15 Desember 2011, 21:21
 */
package Boundary;

import Controller.DatabaseHandler;
import Controller.dataBasePesan;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  Jeanot Nahasan
 */
public class PanelKirimPesan extends javax.swing.JPanel {

    private boolean tabelMemberClick = false;
    boolean tableResipienClick = false;
    DefaultTableModel tabelMemberModel = new DefaultTableModel();
    DefaultTableModel tabelResipienModel = new DefaultTableModel();
    String[] identifierResipien = {"Nama", "No Hp"};


    /** Creates new form PanelKirimPesan */
    public PanelKirimPesan() {
        initComponents();

        this.setSize(960, 420);
        setTabelResipienModel();
        this.panelResipien.setVisible(false);
    }

    public void setTabelResipienModel() {
        tabelResipienModel.setColumnIdentifiers(identifierResipien);
        tabelResipienTerpilih.setModel(tabelResipienModel);
    }

    private boolean isTableResipenMouseClick() {

        return tableResipienClick;
    }

    private boolean isTableMemberMouseClick() {

        return tabelMemberClick;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txfSubject = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txfIsiPesan = new javax.swing.JTextArea();
        btnKirim = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblIconPesan = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        panelResipien = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelMember = new javax.swing.JTable();
        btnTambah = new javax.swing.JButton();
        labelStatus = new javax.swing.JLabel();
        btnKurang = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabelResipienTerpilih = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        btnResipien = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txfSubject.setFont(new java.awt.Font("Tahoma", 0, 14));
        add(txfSubject, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 190, 200, -1));

        txfIsiPesan.setColumns(20);
        txfIsiPesan.setFont(new java.awt.Font("Tahoma", 0, 14));
        txfIsiPesan.setRows(5);
        jScrollPane1.setViewportView(txfIsiPesan);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 230, 280, 160));

        btnKirim.setText("Kirim");
        btnKirim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKirimActionPerformed(evt);
            }
        });
        add(btnKirim, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 400, 70, 30));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Isi Pesan");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 230, -1, -1));

        lblStatus.setForeground(new java.awt.Color(255, 255, 255));
        lblStatus.setText("Status");
        add(lblStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 510, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Kirim Pesan");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, -1, -1));

        lblIconPesan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/Contact.png"))); // NOI18N
        add(lblIconPesan, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Subject");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 190, -1, -1));

        panelResipien.setBackground(new java.awt.Color(0, 102, 255));
        panelResipien.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resipien", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));
        panelResipien.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tabelMember.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nama", "No Hp"
            }
        ));
        tabelMember.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelMemberMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tabelMemberMouseExited(evt);
            }
        });
        jScrollPane2.setViewportView(tabelMember);

        panelResipien.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 430, 80));

        btnTambah.setText("Tambah");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });
        panelResipien.add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        labelStatus.setForeground(new java.awt.Color(255, 255, 255));
        labelStatus.setText("Status :");
        panelResipien.add(labelStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, -1, -1));

        btnKurang.setText("Kurang");
        btnKurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKurangActionPerformed(evt);
            }
        });
        panelResipien.add(btnKurang, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 270, -1, -1));

        tabelResipienTerpilih.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tabelResipienTerpilih.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelResipienTerpilihMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabelResipienTerpilih);

        panelResipien.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 430, 80));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Penerima Pesan");
        panelResipien.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, -1));

        add(panelResipien, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 160, 470, 340));

        btnResipien.setText("Pilih Resipien");
        btnResipien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResipienActionPerformed(evt);
            }
        });
        add(btnResipien, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 400, 110, 30));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/bg.jpg"))); // NOI18N
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 960, 540));
    }// </editor-fold>//GEN-END:initComponents
    private void btnResipienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResipienActionPerformed
        if (btnResipien.getText().equalsIgnoreCase("Pilih Resipien")) {
            panelResipien.setVisible(true);
            showTabelMember();
            btnResipien.setText("Sembunyikan Panel Resipien");
        } else {
            panelResipien.setVisible(false);
            btnResipien.setText("Pilih Resipien");
        }
        
    }//GEN-LAST:event_btnResipienActionPerformed

    public void showTabelMember() {
        try {
            String query = "select * from member order by id";
            PreparedStatement psmt = DatabaseHandler.getDBConnection().prepareStatement(query);


            ResultSet rs = psmt.executeQuery();
            LinkedList x = new LinkedList();

            while (rs.next()) {

                x.add(rs.getString("namamember"));
                x.add(rs.getString("nohpmember"));

            }


            String[] identifier = {"Nama", "No Hp"};
            tabelMemberModel.setColumnIdentifiers(identifier);


            while (!x.isEmpty()) {
                String[] data = new String[2];
                for (int i = 0; i < data.length; i++) {
                    data[i] = String.valueOf(x.remove());
                }

                tabelMemberModel.addRow(data);
            }
            tabelMember.setModel(tabelMemberModel);

        } catch (SQLException ex) {
            Logger.getLogger(PanelKirimPesan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void btnKirimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKirimActionPerformed
        dataBasePesan pesan = new dataBasePesan();

        if (txfIsiPesan.getText().length() > 0 && txfSubject.getText().length() > 0 && tabelResipienTerpilih.getRowCount() > 0) {
            try {
                int noPesan = pesan.autoGenerateNomorPesan();
                pesan.savePesanToDB(noPesan, txfSubject.getText(), txfIsiPesan.getText());
                String noHp;
                for(int i = 0; i < tabelResipienTerpilih.getRowCount(); i++) {
                    noHp = tabelResipienTerpilih.getValueAt(i, 1).toString().trim();
                    pesan.kirimPesan(noPesan, noHp);
                }
                txfSubject.setText("");
                txfIsiPesan.setText("");
                lblStatus.setIcon(null);
                lblStatus.setText("Status : pesan berhasil disimpan");
            } catch (SQLException ex) {
                Logger.getLogger(PanelKirimPesan.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            if (txfSubject.getText().length() == 0) {
                lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
                lblStatus.setText("Status : pesan gagal disimpan! subject pesan tidak boleh kosong !");
            }
            if (tabelResipienTerpilih.getRowCount() == 0) {
                lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
                lblStatus.setText("Status : pesan gagal disimpan! belum ada resipien !");
            } if (txfIsiPesan.getText().length() == 0) {
                lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
                lblStatus.setText("Status : pesan gagal disimpan! isi pesan tidak boleh kosong !");
            }
        }
    // TODO add your handling code here:
    }//GEN-LAST:event_btnKirimActionPerformed

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed

        if (isTableMemberMouseClick() == true) {
            if (tabelMember.getRowCount() == 0) {
                labelStatus.setText("Tidak ada member");
            } else if (tabelMember.getRowCount() > 0) {
                int posR = tabelMember.getSelectedRow();
                String nama = tabelMember.getValueAt(posR, 0).toString().trim();
                String noHp = tabelMember.getValueAt(posR, 1).toString().trim();
                Object data[] = {nama, noHp};
                tabelResipienModel.addRow(data);
                tabelMemberModel.removeRow(posR);
                labelStatus.setText("Menambahkan member " + nama + " ke daftar resipien");
            }
        } else {
            labelStatus.setText("Pilih membernya dulu loh..");
        }
    }//GEN-LAST:event_btnTambahActionPerformed

    private void btnKurangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKurangActionPerformed
        if (isTableResipenMouseClick() == true) {
            if (tabelResipienTerpilih.getRowCount() == 0) {
                labelStatus.setText("Belum ada member");
            } else {
                int posR = tabelResipienTerpilih.getSelectedRow();
                String nama = tabelResipienTerpilih.getValueAt(posR, 0).toString().trim();
                String noHp = tabelResipienTerpilih.getValueAt(posR, 1).toString().trim();
                Object data[] = {nama, noHp};
                tabelMemberModel.addRow(data);
                tabelResipienModel.removeRow(posR);
                labelStatus.setText("Batal memasukan member " + nama + " dari daftar resipien");
            }
        } else {
            labelStatus.setText("Pilih membernya dulu loh..");
        }
    }//GEN-LAST:event_btnKurangActionPerformed

    private void tabelMemberMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMemberMouseClicked
        tabelMemberClick = true;        // TODO add your handling code here:
    }//GEN-LAST:event_tabelMemberMouseClicked

    private void tabelResipienTerpilihMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelResipienTerpilihMouseClicked
        setTableResipienClick(true);        // TODO add your handling code here:
    }//GEN-LAST:event_tabelResipienTerpilihMouseClicked

    private void tabelMemberMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMemberMouseExited
    // TODO add your handling code here:
    }//GEN-LAST:event_tabelMemberMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnKirim;
    private javax.swing.JButton btnKurang;
    private javax.swing.JButton btnResipien;
    private javax.swing.JButton btnTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelStatus;
    private javax.swing.JLabel lblIconPesan;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JPanel panelResipien;
    private javax.swing.JTable tabelMember;
    private javax.swing.JTable tabelResipienTerpilih;
    private javax.swing.JTextArea txfIsiPesan;
    private javax.swing.JTextField txfSubject;
    // End of variables declaration//GEN-END:variables
    public void setTabelMemberClick(boolean tabelMemberClick) {
        this.tabelMemberClick = tabelMemberClick;
    }

    public void setTableResipienClick(boolean tableResipienClick) {
        this.tableResipienClick = tableResipienClick;
    }
}
